//
//  MenusCell.m
//  Menu
//
//  Created by mc on 16/4/15.
//  Copyright © 2016年 xmj. All rights reserved.
//

#import "MenusCell.h"
#import "Menus.h"
#import <UIImageView+WebCache.h>
#import "View+MASAdditions.h"

/** 标题字体 */
#define kNameFont   [UIFont systemFontOfSize:16]
/** 正文字体 */
#define kTextFont   [UIFont systemFontOfSize:14]
#define IPHONE6_SIZE(X)  ([UIScreen mainScreen].bounds.size.width/(750.0/2.0))*X
@interface MenusCell()
@property (nonatomic, strong) UIImageView *pictureView;
@property (nonatomic, strong) UILabel *titleView;
@property (nonatomic, strong) UILabel *textView;

@end

@implementation MenusCell

- (UILabel *)textView{
    if (_textView ==nil) {
        _textView = [[UILabel alloc]init];
        _textView.font = kTextFont;
        _textView.numberOfLines = 0;
        NSDictionary *textDict = @{NSFontAttributeName: kTextFont};
        [self.menus.intro boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:textDict context:nil];
        // _textView.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width;;
        [self.contentView addSubview:_textView];
    }
    return _textView;
}

- (UIImageView *)pictureView{
    if (_pictureView ==nil) {
        _pictureView = [[UIImageView alloc]init];
        [self.contentView addSubview:_pictureView];
    }
    return _pictureView;
}

- (UILabel *)titleView{
    if (_titleView ==nil) {
        _titleView = [[UILabel alloc]init];
        _titleView.font = kNameFont;
       
        [self.contentView addSubview:_titleView];
    }
    return _titleView;
}
- (void)setMenus:(Menus *)menus
{
    _menus = menus;
    
    // 1> 设置数据
    [self settingData];
    
    // 2> 设置位置
    [self settingFrame];
}

- (void)settingData
{
    
    // 正文
    self.textView.text = self.menus.intro;
    [self.pictureView sd_setImageWithURL:[NSURL URLWithString:self.menus.albums]];
    self.titleView.text = self.menus.title;
  
}
- (void)settingFrame
{
    [self.pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top);
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
        make.bottom.equalTo(self.titleView.mas_top).with.offset(-5);
//        #define IPHONE6_SIZE(X)  ([UIScreen mainScreen].bounds.size.width/(750.0/2.0))*X
//        NSNumber *number = [NSNumber numberWithDouble:IPHONE6_SIZE(180)];
//        make.height.equalTo(number);
        
    }];
    
    
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
        
        make.height.equalTo(@20);
    }];
   // self.titleView.backgroundColor = [UIColor redColor];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
        make.top.equalTo(self.titleView.mas_bottom).with.offset(3);
        make.bottom.equalTo(self.contentView).with.offset(-5);
    }];
    
   // [self.textView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
}

+ (instancetype)menusCellWithTableView:(UITableView *)tableView
 {
     static NSString *ID = @"menusCell";
     MenusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
     if (cell == nil) {
        cell = [[MenusCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
     }
    return cell;
 }

+ (CGFloat)height:(Menus *)menus{
                       
    // sizeWithFont:kTextFont constrainedToSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, MAXFLOAT)];
    CGSize textSize = [menus.intro boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
   // sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
      CGFloat cellHeight = IPHONE6_SIZE(180) + 20 + textSize.height + 14;
    return cellHeight;
}


@end
